#include "StdAfx.h"
#include "BatteryDevice.h"
#include "RobotInstance.h"

namespace RobotPlant { namespace RobotProxy{

	BatteryDevice::BatteryDevice(RobotInstance* pParent,joint::spawn_type e):DeviceBase(pParent,e)
	{
		_deviceType = DeviceTypeEnum::Battery;
		_deviceIndex = 1;
		chord(GetMaxBatteryPower, &BatteryDevice::Handler_GetMaxBatteryPower);
		chord(GetPercentBatteryPower, &BatteryDevice::Handler_GetPercentBatteryPower);
		chord(GetPercentCriticalBattery, &BatteryDevice::Handler_GetPercentCriticalBattery);
		chord(SetPercentCriticalBattery, &BatteryDevice::Handler_SetPercentCriticalBattery);
	}

	BatteryDevice::~BatteryDevice()
	{
	}

	double BatteryDevice::Handler_GetMaxBatteryPower(void_t)
	{
		boost::mutex::scoped_lock lock(this->_pParent->_mutex);
	
		double value = 0;

		RobDeviceInterface robDeviceInterface;
		string addr = _pParent->_soapServer;
		robDeviceInterface.endpoint = addr.data();
		if (robDeviceInterface.ns1__Battery_GetMaxBatteryPower(this->_deviceIndex,&value) != SOAP_OK)
		{
			value = -1;
		}

		return value;
	}

	double BatteryDevice::Handler_GetPercentBatteryPower(void_t)
	{
		boost::mutex::scoped_lock lock(this->_pParent->_mutex);
	
		double value = 0;

		RobDeviceInterface robDeviceInterface;
		string addr = _pParent->_soapServer;
		robDeviceInterface.endpoint = addr.data();
		if (robDeviceInterface.ns1__Battery_GetPercentBatteryPower(this->_deviceIndex,&value) != SOAP_OK)
		{
			value = -1;
		}

		return value;
	}

	double BatteryDevice::Handler_GetPercentCriticalBattery(void_t)
	{
		boost::mutex::scoped_lock lock(this->_pParent->_mutex);

		double value = 0;

		RobDeviceInterface robDeviceInterface;
		string addr = _pParent->_soapServer;
		robDeviceInterface.endpoint = addr.data();
		if (robDeviceInterface.ns1__Battery_GetPercentCriticalBattery(this->_deviceIndex,&value) != SOAP_OK)
		{
			value = -1;
		}

		return value;
	}

	void BatteryDevice::Handler_SetPercentCriticalBattery(double criticalBattery)
	{
		boost::mutex::scoped_lock lock(this->_pParent->_mutex);

		RobDeviceInterface robDeviceInterface;
		string addr = _pParent->_soapServer;
		robDeviceInterface.endpoint = addr.data();
		ns1__EmptyStruct ret;
		if (robDeviceInterface.ns1__Battery_SetPercentCriticalBattery(this->_deviceIndex, criticalBattery, &ret) != SOAP_OK)
		{
			//value = -1;
		}

	}
}}